const { createConnection } = require('../config/dbConfig');
const userDBConfig = require('../config/userDBConfig');

const login = async (username, password) => {
    const connection = await createConnection(userDBConfig);
    const [rows] = await connection.execute('SELECT * FROM users WHERE username = ? AND password = ?', [username, password]);
    connection.end();
    return rows[0];
};

const register = async (user) => {
    const connection = await createConnection(userDBConfig);
    const [result] = await connection.execute('INSERT INTO users (username, email, password, isAdmin) VALUES (?,?,?,?)', [
        user.username,
        user.email,
        user.password,
        user.isAdmin
    ]);
    connection.end();
    return result.insertId;
};

module.exports = {
    login,
    register
};